﻿using AutoMapper;
using D.Util.UtilConsole;

namespace D.Util.UtilAutoMapper
{
    /// <summary>
    /// 继承自Profile类
    /// </summary>
    public class MappingProfile : Profile
    {
        public MappingProfile()
        {

        }

        /// <summary>
        /// 构造函数中实现映射
        /// </summary>
        /// <param name="dtoToEntity">视图类转实体类</param>
        /// <param name="entityToDto">实体类转视图类</param>
        public MappingProfile(Dictionary<Type, Type> dtoToEntity, Dictionary<Type, Type> entityToDto = null)
        {
            #region DTO映射到实体

            ConsoleUtil.Info("DTO to Entity：");

            if (dtoToEntity != null)
            {
                foreach (var item in dtoToEntity)
                {
                    ConsoleUtil.Waring($"{item.Key} => {item.Value}"); 
                    CreateMap(item.Key, item.Value);
                }
            }
            else
            {
                ConsoleUtil.Err("暂无DTO to Entity数据映射！");
            }
            #endregion

            #region 从Student映射到StudentDTO

            ConsoleUtil.Info("Entity to DTO：");
            if (entityToDto != null)
            {
                foreach (var item in entityToDto)
                {
                    ConsoleUtil.Waring($"{item.Key} => {item.Value}");
                    // DTO映射到实体,在映射的最后使用ReverseMap()方法
                    CreateMap(item.Key, item.Value).ReverseMap();
                }
            }
            else
            {
                ConsoleUtil.Err("暂无DTO to Entity数据映射！");
            }
            #endregion
        }
    }
}
